package abel.wlmr.model.type;

import abel.wlmr.model.WlmrException;
import abel.wlmr.model.WlmrField;

public class BaudPortField extends WlmrField {

    private BaudType baud = BaudType.BaudDefault;

    private PortType port = PortType.PortCarrier;

    public BaudPortField() {
        super(1);
    }

    @Override
    public int getValue() {
        return baud.getValue() << 5 | port.getValue();
    }

    @Override
    public void parseValue(int v) throws WlmrException {
        baud = BaudType.findBaudByValue((byte) ((v & 0xE0) >> 5));

        port = PortType.findPortByValue((byte) (v & 0x1F));
    }

    /**
     * @return the baud
     */
    public BaudType getBaud() {
        return baud;
    }

    /**
     * @param baud
     *            the baud to set
     */
    public void setBaud(BaudType baud) {
        if (baud == null) {
            throw new IllegalArgumentException("baud can't be null");
        }
        this.baud = baud;
    }

    /**
     * @return the port
     */
    public PortType getPort() {
        return port;
    }

    /**
     * @param port
     *            the port to set
     */
    public void setPort(PortType port) {
        if (port == null) {
            throw new IllegalArgumentException("port can't be null");
        }
        this.port = port;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("BaudPortField [baud=%s, port=%s]", baud, port);
    }

}
